<script>
export default {
  Username: '',
  Name: '',
  Token: '',
  City: '榆林市',
  Location: '111.016431,39.015658',
  IsLogin: function () {
    return this.Token.length > 1
  },
  getCityCode:function () {
    var currCityName=this.City
    var cityCode=''
    dict.CoverCity.forEach(function (province, index) {
      province.list.forEach(function (city,_index) {
        if (city.name===currCityName){
          cityCode=city.cityCode
        }
      })
    })
    return cityCode
  },
  // 检查用户缓存状态
  checkCache: function () {
    // console.log('检查用户状态')
    var localString = localStorage.getItem('shopClerkUser')

    if (localStorage.getItem('currCity')){
      this.City=localStorage.getItem('currCity')
    }
    if (localStorage.getItem('currLocation')){
      this.Location=localStorage.getItem('currLocation')
    }

    if (typeof localString !== 'string') {
      return false
    }
    if (localString === null || localString === '') {
      return false
    }
    var obj = JSON.parse(localString)
    this.Token = obj.token
    this.Name = obj.name
    this.Username = obj.username
    // console.log(obj)
  },
  getToken:function () {
    var localString = localStorage.getItem('shopClerkUser')
    // console.log('localString',localString)
    var obj = JSON.parse(localString)
    return obj.token
  },
  // 设置用户状态
  setCache: function (data) {
    // console.log(data)
    if (data === null) {
      localStorage.removeItem('shopClerkUser')
      this.Username = ''
      this.Token = ''
      this.Name = ''
      return
    }
    localStorage.setItem('shopClerkUser', JSON.stringify(data))
    this.checkCache()
  },
  setCity:function (city) {
    this.City=city
    localStorage.setItem('currCity', city)
  },
  setLocation:function (location) {
    this.Location=location
    localStorage.setItem('currLocation', location)
  }
}
</script>
